package org.example.business.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import org.example.business.feign.AccountFeign;
import org.example.business.feign.GoodsFeign;
import org.example.business.service.BusinessService;
import org.example.business.vo.PlaceOrderVo;
import org.example.entity.Account;
import org.example.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private AccountFeign accountFeign;

    @Autowired
    private GoodsFeign goodsFeign;

    @GlobalTransactional
    @Override
    public void placeOrder(PlaceOrderVo placeOrderVo) {
        Account account = accountFeign.queryById(placeOrderVo.getAccountId());
        Goods goods = goodsFeign.queryById(placeOrderVo.getGoodsId());
        accountFeign.reduceAccountMoney(placeOrderVo.getAccountId(), placeOrderVo.getAmount() * goods.getGoodsPrice());
        goodsFeign.reduceGoods(placeOrderVo.getGoodsId(), placeOrderVo.getAmount());

//        int i = 1 / 0;

        System.out.println("account = " + account);
    }
}
